<?php

class WebsiteController extends AdminController{
    public $parentUrl;
    
    public function init(){
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }
    
    public function actionIndex(){
        try {
            $model = Website::model()->findAll();
            $this->render('index', array(
                'data' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
    
    public function actionCreate(){
        try {
            $model = new Website;
            if(isset($_POST['Website'])){
                $model->attributes = $_POST['Website'];
                $model->name = $_POST['Website']['name'];
                $model->link = $_POST['Website']['link'];
                $model->validate();
                if(!$model->hasErrors()){
                    if($model->save()){
                        $this->redirect(Yii::app()->createAbsoluteUrl('systems/website/index'));
                    }
                }
            }
            $this->render('create', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id){
        try {
            $model = $this->loadModel($id);
            if(isset($_POST['Website'])){
                $model->attributes = $_POST['Website'];
                $model->name = $_POST['Website']['name'];
                $model->link = $_POST['Website']['link'];
                $model->validate();
                if(!$model->hasErrors()){
                    if($model->save()){
                        $this->redirect(Yii::app()->createAbsoluteUrl('systems/website/index'));
                    }
                }
            }
            $this->render('update', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
    
    public function actionDelete($id){
        if($model = $this->loadModel($id)){
            if($model->delete()){
                $this->redirect(Yii::app()->createAbsoluteUrl('systems/website/index'));
            }
        }
    }

    public function loadModel($id){
        try {
            $model = Website::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
}
?>
